#include <iostream>
#include <vector>
#include <string>
#include <climits>
using namespace std;

int test() 
{
    int n = 0;
    cin >> n;
    string str1, str2;
    cin >> str1 >> str2;
    vector<string> strs;
    strs.resize(n);
    for (size_t i = 0; i < n; ++i) {
        strs[i].reserve(10);
        cin >> strs[i];
    }
    int ans = INT_MAX;
    vector<string>::iterator it1 = find(strs.begin(), strs.end(), str1);
    vector<string>::iterator it2 = find(strs.begin(), strs.end(), str2);
    if (it1 == strs.end() || it2 == strs.end())
        return -1;
    else
    {
        while (it1 != strs.end())
        {
            it2 = find(strs.begin(), strs.end(), str2);
            while (it2 != strs.end())
            {
                ans = (int)fmin(ans, abs(it1 - it2));
                it2 = find(it2 + 1, strs.end(), str2);
            }
            it1 = find(it1 + 1, strs.end(), str1);
        }
    }
    return ans;
}


int main()
{
    int ans = test();
    cout << ans << endl;
	return 0;
}